package com.lpit.controller;

import com.lpit.pojo.Susceptible;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/*@Slf4j
@Api(tags = "敏感词接口")*/
/*
* 敏感词管理
* */
@RestController
@RequestMapping("/Susceptible")
public class SusceptibleController {
    @Autowired
    private RedisTemplate redisTemplate;

    /*添加敏感词*/
    /*@PostMapping*/
    @PostMapping
    @ApiOperation("新增敏感词")
    public void  addSusceptible(String value){
        //添加数据到redis内
        redisTemplate.opsForSet().add("Susceptible", value);
        inquireSusceptible();

    }
    /*删除敏感词*/
//    @GetMapping
//    public void deleteSusceptible(String value){
//        System.out.println("进入删除敏感词");
//        //删除指定的敏感词
//        redisTemplate.opsForSet().remove("Susceptible", value);
//        System.out.println("退出删除敏感词");
//    }
    /* 查询敏感词 */
    @GetMapping
    @ApiOperation("查询敏感词")
    public List<Susceptible>  inquireSusceptible (){
        //redis取出来的值  (查出susceptible内的所有值)
        Set set = redisTemplate.opsForSet().members("Susceptible");
        //List
        List<Susceptible> susceptibleList = new  ArrayList<>();
        for(Object aa: set){
            //敏感词实体类
            Susceptible susceptible = new Susceptible();
            // 添加数据
            susceptible.setSusceptibleName(aa.toString());
            //将敏感词实体类放入List集合
            susceptibleList.add(susceptible);
        }
        System.out.println(susceptibleList);
        return susceptibleList;
    }
}
